package com.bruce.demo2.patterns.demo2.structural.demo12.flyweight.demo2;

import java.util.HashMap;
import java.util.Map;

/**
 * @PROJECT_NAME: design_patterns
 * @DESCRIPTION: 享元工厂方法
 * @AUTHOR: CNBruceLee
 * @DATE: 2021/8/2 22:49
 */
public class FlyweightFactory {
    private Map<String, Flyweight> flyweightMap;


    /**
     * 私有化构造方法
     */
    private FlyweightFactory() {
        flyweightMap = new HashMap<String, Flyweight>();
        flyweightMap.put("shot", new shotFlyweight());
        flyweightMap.put("speed", new SpeedFlyweight());
    }

    /**
     * 但是模式工厂方法
     */
    private static FlyweightFactory flyweightFactory = new FlyweightFactory();

    /**
     * 获取工厂方法
     *
     * @return
     */
    public static FlyweightFactory getInstance() {
        return flyweightFactory;
    }

    /**
     * 获取享元对象
     *
     * @param key
     * @return
     */
    public Flyweight getFlyweight(String key) {
        return flyweightMap.get(key);
    }
}
